﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hello_accinv
{
    public partial class ChooseProduct : Form
    {
        private Database db;
        private bool initializing;
        public string selectedProductCode;

        public ChooseProduct()
        {
            InitializeComponent();
            db = new Database();
        }

        private void ChooseProduct_Load(object sender, EventArgs e)
        {
            LoadCategoryList();
        }

        private void LoadCategoryList()
        {
            initializing = true;

            DataTable dt = db.Select("Name", "Category", "1 ORDER BY Name ASC");
            List<string> lst = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lst.Add(dt.Rows[i]["Name"].ToString());
            }
            cmbCategory.DataSource = lst;
            cmbCategory.SelectedIndex = -1;

            initializing = false;
        }

        private void datagridProduct_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (datagridProduct.SelectedRows.Count == 1)
            {
                btnOK.Enabled = true;
            }
            else
            {
                btnOK.Enabled = false;
            }
        }

        private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!initializing && cmbCategory.SelectedIndex != -1)
            {
                datagridProduct.DataSource =
                    db.Select("PCode, Product.Name, Quantity",
                              "Product INNER JOIN Category ON Product.CategoryID = Category.CategoryID",
                              "Product.Flag = '1' AND Category.Name = '" + cmbCategory.SelectedItem.ToString() + "'");
                datagridProduct.ClearSelection();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ChoosingProduct();
        }

        private void ChoosingProduct()
        {
            if (Int32.Parse(datagridProduct.SelectedRows[0].Cells[2].Value.ToString()) > 0)
            {
                selectedProductCode = datagridProduct.SelectedRows[0].Cells[0].Value.ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Dispose();
            }
            else
            {
                MessageBox.Show("Product:\t" + datagridProduct.SelectedRows[0].Cells[1].Value + "\nCategory:\t" + cmbCategory.SelectedItem + "\n\nNo product in stock", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                datagridProduct.ClearSelection();
            }
        }

        private void datagridProduct_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            ChoosingProduct();
        }
    }
}
